package com.java.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.java.dao.TodoDao;
import com.java.entity.TodoList;
import com.java.ext.MyButton;
import com.java.ext.MyJTable;
import com.java.model.TodoTableModel;
import com.java.utils.MyWindowListener;

/**
* @ClassName: AdminMainWindow
* @Description: 管理员登陆成功主界面
*
 */
public class AdminMainWindow extends BaseJFrame {
	private static final long serialVersionUID = 1L;

	private List<TodoList> todos = new ArrayList<TodoList>();

	private TodoList selectObj = null;

	private JTable table = null;

	public AdminMainWindow() {
		setTitle("To Do Management System");
		setSize(700, 480);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);  
        addWindowListener(new CloseButtonListener()); 
		setLocationRelativeTo(null);
		initMenu(this);

		todos = TodoDao.getTodos();

		TodoTableModel md = new TodoTableModel(todos);
		table = new MyJTable(md);
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);

		// 搜索框面板
		JPanel searchPanel = new JPanel();
		searchPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        final JTextField searchField = new JTextField(13);
        searchField.addFocusListener(new FocusListener() {
			@Override
			public void focusGained(FocusEvent e) {
				 if (searchField.getText().equals("Input title...")){
					 searchField.setText("");
					 searchField.setForeground(Color.black);
                }
			}

			@Override
			public void focusLost(FocusEvent e) {
				if (searchField.getText().equals("")){
					searchField.setForeground(Color.gray);
                    searchField.setText("Input title...");
                }
			}
        	
        });
        searchField.setForeground(Color.gray); //将提示文字设置为灰色
        searchField.setText("Input title...");
        MyButton searchButton = new MyButton("Search").defaultt();
        MyButton drawButton = new MyButton("Calendar").defaultt();
        drawButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setEnabled(false);
				new DrawStart().start();
				new TodoDrawWindow(bJFrame);
			}
        	
        });
		searchPanel.add(searchField);
		searchPanel.add(searchButton);
		searchPanel.add(drawButton);
		add(searchPanel, BorderLayout.NORTH);
		
		// 创建输入框和按钮
		JPanel jPanel = new JPanel();
		jPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		final MyButton updateButton = new MyButton("modify").warning();
		final MyButton deleteButton = new MyButton("Delete").danger();
		jPanel.add(updateButton);
		jPanel.add(deleteButton);
		add(jPanel, BorderLayout.SOUTH);
		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (null == selectObj) {
					error("Not Selected");
					return;
				}
				new AddTodoWindow(bJFrame, selectObj);
			}
		});
		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (null == selectObj) {
					error("Not Selected");
					return;
				}
				String title = selectObj.getTitle();
				if(title.length() > 10) {
					title = title.substring(0, 10) + "...";
				}
				if (confirm("Confirm deleting 【" + title + "】")) {
					TodoDao.delTodo(selectObj.getTitle());
					todos = TodoDao.getTodos();
					table.setModel(new TodoTableModel(todos));
				}
			}
		});

		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					int row = table.rowAtPoint(e.getPoint());
					selectObj = getObj(row);
					updateButton.setEnabled(true);
					deleteButton.setEnabled(true);
				}
				if (e.getClickCount() == 2) {
					setEnabled(false);
					new AddTodoWindow(bJFrame, selectObj);
				}
			}
		});

		setVisible(true);
	}

	private TodoList getObj(int index) {
		for (int i = 0; i < todos.size(); i++) {
			if (index == i) {
				return todos.get(i);
			}
		}
		return null;
	} 
    
    class CloseButtonListener extends MyWindowListener {
    	
    	@Override
    	public void windowClosing(WindowEvent e) {  
    		exitSys();
        }
    }

}